package com.hunan.yllxy.repostitory.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hunan.yllxy.repostitory.dao.DakaOptionInfoDao;
import com.hunan.yllxy.repostitory.dao.DakaScoreInfoDao;
import com.hunan.yllxy.repostitory.dao.UserDakaInfoDao;
import com.hunan.yllxy.repostitory.entity.DakaContentInfo;
import com.hunan.yllxy.repostitory.entity.DakaOptionInfo;
import com.hunan.yllxy.repostitory.entity.DakaScoreInfo;
import com.hunan.yllxy.repostitory.entity.UserDakaInfo;
import com.hunan.yllxy.repostitory.mapper.DakaContentInfoMapper;
import com.hunan.yllxy.repostitory.dao.DakaContentInfoDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hunan.yllxy.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘华
 * @since 2025-06-20
 */
@Service
public class DakaContentInfoDaoImpl extends ServiceImpl<DakaContentInfoMapper, DakaContentInfo> implements DakaContentInfoDao {
    @Autowired
    private DakaScoreInfoDao dakaScoreInfoDao;
    @Autowired
    private DakaOptionInfoDao dakaOptionInfoDao;
    @Autowired
    private UserDakaInfoDao userDakaInfoDao;
    @Override
    public List<DakaContentInfo> findByActivityId(String activityId) {
        LambdaQueryWrapper<DakaContentInfo> query = Wrappers.lambdaQuery(this.entityClass);
        query.eq(DakaContentInfo::getActivityId,activityId);
        query.orderByAsc(DakaContentInfo::getContentIndex);
        return baseMapper.selectList(query);
    }

    @Override
    public DakaContentInfo dakaContent(String activityId, String id) {
        return baseMapper.dakaContent(activityId,id);
    }

    @Override
    public List<DakaContentInfo> dakaRecord(String userId, String activityId) {
        List<DakaContentInfo> dakaContentInfos = baseMapper.dakaRecord(userId, activityId);
        for (DakaContentInfo dakaContentInfo : dakaContentInfos) {
            List<UserDakaInfo> userDakaInfoList = userDakaInfoDao.findByContentIdList(userId, dakaContentInfo.getId());
            dakaContentInfo.setSfdk(StringUtils.isNotNull(userDakaInfoList));

            if (StringUtils.isNotNull(userDakaInfoList)){
                for (UserDakaInfo userDakaInfo : userDakaInfoList) {
                    DakaScoreInfo scoreInfo = dakaScoreInfoDao.getById(userDakaInfo.getDakaScoreId());
                    DakaOptionInfo optionInfo = dakaOptionInfoDao.getById(userDakaInfo.getDakaOptionId());
                    userDakaInfo.setDakaScoreInfo(scoreInfo);
                    userDakaInfo.setDakaOptionInfo(optionInfo);
//                    userDakaInfo.setDakaContentInfo(dakaContentInfo);

                }
            }
            dakaContentInfo.setUserDakaInfoList(userDakaInfoList);
        }
        return dakaContentInfos;
    }
}
